package br.finanso.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import br.finanso.base.dao.model.UsuarioDAO;
import br.finanso.language.LanguageGen;
import br.finanso.model.Usuario;

public class FrameLogin extends JFrame{	
	private static final long serialVersionUID = 1L;
	
	private JTextField tFUsuario;
	private JPasswordField tFSenha;
	private JButton btEntrar;
	
	public FrameLogin() {
		initFrame();
	} 
	private void initFrame(){
		try{
			this.setIconImage(new ImageIcon(getClass().getResource("/logo_login.png")).getImage());
		}catch(Exception e){
			e.printStackTrace();
		}
		this.setResizable(false);
		this.setSize(new Dimension(new Dimension(300, 300)));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle(LanguageGen.FRAME_LOGIN_TITLE);
		this.setLayout(new BorderLayout());
		this.setLocationRelativeTo(null);
		
		JPanel panelNort = new JPanel();
		panelNort.setBackground(Color.WHITE);
		try{
			panelNort.add(new JLabel(new ImageIcon(getClass().getResource("/logo_login_top.png"))));
		}catch(Exception e){
			e.printStackTrace();
		}
		this.add(panelNort, BorderLayout.NORTH);
		
		JPanel panel = new JPanel(new GridBagLayout());
		
		JLabel labelUsuario = new JLabel(LanguageGen.FRAME_LOGIN_TF_USUARIO);
		labelUsuario.setFont(new Font("Arial", Font.BOLD, 21));
		GridBagConstraints grid = new GridBagConstraints();
		grid.gridx = 0;
		grid.gridy = 0;
		grid.insets = new Insets(1, 2, 1, 2);
		grid.fill = 1;
		panel.add(labelUsuario,grid);
		
		tFUsuario = new JTextField();
		tFUsuario.setPreferredSize(new Dimension(150,30));
		tFUsuario.setFont(new Font("Arial", Font.BOLD, 21));
		grid = new GridBagConstraints();
		grid.gridx = 1;
		grid.gridy = 0;
		grid.insets = new Insets(1, 2, 1, 2);
		grid.fill = 1;
		panel.add(tFUsuario,grid);
		
		JLabel labelSenha = new JLabel(LanguageGen.FRAME_LOGIN_TF_SENHA);
		labelSenha.setFont(new Font("Arial", Font.BOLD, 18));
		grid = new GridBagConstraints();
		grid.gridx = 0;
		grid.gridy = 1;
		grid.insets = new Insets(1, 2, 1, 2);
		grid.fill = 1;
		panel.add(labelSenha,grid);
		
		tFSenha = new JPasswordField();
		tFSenha.setPreferredSize(new Dimension(150,30));
		tFSenha.setFont(new Font("Arial", Font.BOLD, 18));
		grid = new GridBagConstraints();
		grid.gridx = 1;
		grid.gridy = 1;
		grid.insets = new Insets(1, 2, 1, 2);
		grid.fill = 1;
		panel.add(tFSenha, grid);
		
		btEntrar = new JButton(LanguageGen.FRAME_LOGIN_BT_ENTRAR);
		btEntrar.setPreferredSize(new Dimension(100, 42));
		grid = new GridBagConstraints();
		grid.gridx = 0;
		grid.gridy = 2;
		grid.insets = new Insets(1, 2, 1, 2);
		grid.fill = 1;
		grid.gridwidth = 2;
		panel.add(btEntrar,grid);
		btEntrar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				entrar();
			}
		});
		this.add(panel, BorderLayout.CENTER);
		
		JLabel labelVersao = new JLabel(LanguageGen.FRAME_LOGIN_VERSAO);
		labelVersao.setFont(new Font("Arial", Font.ITALIC, 10));
		labelVersao.setForeground(Color.GRAY);
		this.add(labelVersao, BorderLayout.SOUTH);
	}
	private void entrar(){
		UsuarioDAO dao = new UsuarioDAO();
		@SuppressWarnings("deprecation")
		Usuario u = dao.validarUsuario(tFUsuario.getText(), tFSenha.getText());
		if(u != null){
			JOptionPane.showMessageDialog(this, "Usuario Valido");
			return;
		}{
			JOptionPane.showMessageDialog(this, "Usuario INVALIDO");
			return;
		}
	}
}
